#!/usr/bin/python
# -*- coding:utf-8 -*-
# @Time  : 6/2/20
# @Author: yanlh
# @usage : 校验/etc/default/docker文件的所有权为root:root，文件权限为644
import docker_utils


def get_plugin_info():
    plugin_info = {
        "name": "Docker_file_004 /etc/default/docker",
        "plugin_id": "docker_file_004",
        "plugin_type": "Docker_File_Safe",
        "info": "Check and set docker file /etc/default/docker",
        "level": "B",
        "module": "Safety reinforcement",
        "author": "yanlh",
        "keyword": "Safety reinforcement",
        "configable": "false",
    }
    return plugin_info


logger = None
cur_user = None
cur_module = None
cur_task = None
path = None


def set_plugin_logger(setter, user, module, task, *args, **kwargs):
    global logger, cur_user, cur_module, cur_task
    logger = setter
    cur_user = user
    cur_module = module
    cur_task = task


def record_logger(record_list, module):
    des_list = []
    for item in record_list:
        des_list.append(item['info'])
        if item['type'] == 'info':
            logger.debug_info(cur_user, cur_module, cur_task + module, '', item['info'])
        if item['type'] == 'error':
            logger.debug_error(cur_user, cur_module, cur_task + module, '', item['info'])
        if item['type'] == 'warning':
            logger.debug_warning(cur_user, cur_module, cur_task + module, '', item['info'])
    return des_list


# 扫描函数
def scan(func_args, flag=0):
    logger.debug_info(cur_user, cur_module, cur_task + '_Scan', '', "Scan Start.")
    record_scan, scan_result = docker_utils.scan_file_config(func_args)
    scan_list = record_logger(record_scan, '_Scan')
    if flag and scan_result['flag']:
        record_rein, error_count = docker_utils.reinforce_file_config(func_args, scan_result)
        rein_list = record_logger(record_rein, '_Reinforce')
        scan_list.extend(rein_list)
        return scan_list, error_count
    return scan_list, scan_result['error_count']


# 回滚函数
def rollback(func_args):
    record_roll, error_count = docker_utils.rollback_file(func_args)
    roll_list = record_logger(record_roll, '_Rollback')
    return roll_list, error_count


def check(ip, *args, **kwargs):
    sys_user = kwargs.get("system_user")
    sys_pwd = kwargs.get("system_pwd")
    comm = kwargs.get("command")
    try:
        logger.debug_info(cur_user, cur_module, cur_task + '_Check', '', "docker_file_004 Start.")
        func_args = {'ip': ip, 'username': sys_user, 'passwd': sys_pwd, 'comm': comm,
                     'owner': 'root:root', 'privilege': '644', 'path': '/etc/default/docker'}
        result = docker_utils.check_func(scan, rollback, func_args)
        logger.debug_info(cur_user, cur_module, cur_task + '_Check', '', "docker_file_004 Complete.")
        return result
    except Exception as er:
        code = 1
        des = ["ERROR:", str(er)]
        logger.debug_error(cur_user, cur_module, cur_task + '_Check', '', des)
        return {"code": code, "count": 0, "des": des}


if __name__ == '__main__':
    """
    本地测试
    """
    check(ip=None, command=2)
